/**
 *  项目列表 - projectlist
 *     项目名      - name
 *     地址        - address
 *     业态        - operation
 *     一级分类列表 - level1list
 *          一级分类名称 - level1
 *          照片描述列表 - photolist
 *              照片描述名称    - label
 *              照片列表        - list
 *                   照片       - item  -->  通过它去localstorage获取base64
 *                                  转换之后是label和src
 *          二级分类列表 - level2list
 *              二级分类名称    - level2
 *              照片描述列表    - photolist  -->  内部结构同上
 */
import configlistjson from '@/json/configlist.json';
import myStorage from '@/utils/myStorage.js';

// 获取配置文件-业态列表，包含三大业态、八大一级分类、预定义的二级分类
export function getConfigList() {
    return JSON.parse(myStorage.getItem('configlist') || "[]");
}
// 设置配置文件-业态列表
export function setConfigList() {
    const configlist = myStorage.getItem('configlist');
    if (!configlist) {
        myStorage.setItem('configlist', JSON.stringify(configlistjson));
    }
}
// 根据业态名称获取一级分类列表
function getLevel1ListByOperation(operation) {
    const configlist = getConfigList();
    const operationObj = configlist.find(t=>t.operation === operation) || {};
    return operationObj.level1list || [];
}
// 获取项目缓存列表
export function getProjectList() {
    return JSON.parse(myStorage.getItem('projectlist') || "[]");
}
// 删除某个项目
export function removeProjectByIndex(index) {
    const projectlist = getProjectList();
    const p = projectlist.splice(index, 1);
    removeProjectImgStorage(p);
    myStorage.setItem('projectlist', JSON.stringify(projectlist));
}
// 新增项目
export function addProject(projectObj) {
    let projectlist = getProjectList();
    projectlist.push({
        ...projectObj,
        level1list: getLevel1ListByOperation(projectObj.operation)
    });
    myStorage.setItem('projectlist', JSON.stringify(projectlist));
}
// 保存一级分类照片描述列表
export function saveLevel1Photo(projectName, level1, photo) {
    let projectlist = getProjectList();
    const projectIndex = projectlist.findIndex(t => t.name === projectName);
    let level1list = projectlist[projectIndex].level1list;
    const level1Index = level1list.findIndex(t => t.level1 === level1);
    const photolist = photo.map((t) => {
        let obj = { label: t.label };
        const list = t.list || [];
        obj.list = list.map((p) => {
            // 不能使用下标来给图片做key了，随机3位数 + 时间戳后6位 + 随机3位数
            // 优化、保存列表的时候就不存储图片了，太费性能
            // myStorage.setItem(item, p.src);
            return { item: p.item };
        })
        return obj;
    });
    level1list[level1Index].photolist = photolist;
    myStorage.setItem('projectlist', JSON.stringify(projectlist));
}
// 保存二级分类照片描述列表
export function saveLevel2Photo(projectName, level1, level2, photo) {
    let projectlist = getProjectList();
    const projectIndex = projectlist.findIndex(t => t.name === projectName);
    let level1list = projectlist[projectIndex].level1list;
    const level1Index = level1list.findIndex(t => t.level1 === level1);
    let level2list = level1list[level1Index].level2list;
    const level2Index = level2list.findIndex(t => t.level2 === level2);
    const photolist = photo.map((t) => {
        let obj = { label: t.label };
        const list = t.list || [];
        obj.list = list.map((p) => {
            // 不能使用下标来给图片做key了，随机3位数 + 时间戳后6位 + 随机3位数
            // 优化、保存列表的时候就不存储图片了，太费性能
            // myStorage.setItem(item, p.src);
            return { item: p.item };
        })
        return obj;
    });
    level2list[level2Index].photolist = photolist;
    myStorage.setItem('projectlist', JSON.stringify(projectlist));
}
export function getLevel1Photo(projectName, level1) {
    let projectlist = getProjectList();
    const projectIndex = projectlist.findIndex(t => t.name === projectName);
    let level1list = projectlist[projectIndex].level1list;
    const level1Index = level1list.findIndex(t => t.level1 === level1);
    const photolist = (level1list[level1Index].photolist || []).map((t) => {
        let obj = { label: t.label };
        const list = t.list || [];
        obj.list = list.map((p) => {
            return { item: p.item, src: myStorage.getItem(p.item) };
        })
        return obj;
    });
    return photolist;
}
export function getLevel2Photo(projectName, level1, level2) {
    let projectlist = getProjectList();
    const projectIndex = projectlist.findIndex(t => t.name === projectName);
    let level1list = projectlist[projectIndex].level1list;
    const level1Index = level1list.findIndex(t => t.level1 === level1);
    let level2list = level1list[level1Index].level2list;
    const level2Index = level2list.findIndex(t => t.level2 === level2);
    const photolist = (level2list[level2Index].photolist || []).map((t) => {
        let obj = { label: t.label };
        const list = t.list || [];
        obj.list = list.map((p) => {
            return { item: p.item, src: myStorage.getItem(p.item) };
        })
        return obj;
    });
    return photolist;
}
// 清除localStorage 和 plus.storage里面的数据
export function resetStorage() {
    myStorage.clear();
    myStorage.setItem('configlist', JSON.stringify(configlistjson));
}
// 清除缓存，先清除所有的图片缓存，再清除项目信息
export function resetStorage1() {
    const projectlist = getProjectList();
    projectlist.forEach((p) => {
        removeProjectImgStorage(p);
    });
    myStorage.removeItem('projectlist');
    myStorage.setItem('configlist', JSON.stringify(configlistjson));
}
// 根据项目obj请求所有的图片缓存
function removeProjectImgStorage(p) {
    /* 获取一级分类列表 */
    const level1list = p.level1list || [];
    level1list.forEach((lv1) => {
        /* 获取一级分类下照片描述列表 */
        const photolist = lv1.photolist || [];
        photolist.forEach((photo) => {
            /* 获取照片描述下照片列表 */
            const list = photo.list || [];
            list.forEach((l) => {
                myStorage.removeItem(l.item);
            })
        })
        /* 获取一级分类下二级分类列表 */
        const level2list = lv1.level2list || [];
        level2list.forEach((lv2) => {
            /* 获取二级分类下照片描述列表 */
            const photolist = lv2.photolist || [];
            photolist.forEach((photo) => {
                /* 获取照片描述下照片列表 */
                const list = photo.list || [];
                list.forEach((l) => {
                    myStorage.removeItem(l.item);
                })
            })
        });
    })
}